<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 共用模块-更新records与任务、接单数量表关系的Ajax调用接口
 */
class UpdateCeping extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
    }

    public function index(){
        $this->load->model('MTaskRecords');
        $this->load->model('MTaskApplyNum');
        $this->load->model('MTask');
        $this->db->trans_start();
        $RA = $this->MTaskRecords->getDiffRA();
        $RT = $this->MTaskRecords->getDiffRT();

        // 清空task 与 ApplyNum完成测评数
        if (!$this->MTaskRecords->clearA()){
            $this->db->_trans_status = FALSE;
        };
        if (!$this->MTaskRecords->clearT()){
            $this->db->_trans_status = FALSE;
        };

        // 更新Records表与ApplyNum表
        foreach ($RA as $v){
            $item = $this->MTaskApplyNum->getById($v->an_id);
            $item->finish_comment_num = $v->num;
            if (!$this->MTaskApplyNum->update($item,$item->update_time)){
                $this->db->_trans_status = FALSE;
            }
        }

        // 更新Task表与ApplyNum表
        foreach ($RT as $v){
            $task = $this->MTask->getById($v->task_id);
            $task->comment_finish_num = $v->num;
            if (!$this->MTask->update($task,$task->update_time)){
                $this->db->_trans_status = FALSE;
            }
        }
        $this->db->trans_complete();
        if ($this->db->_trans_status){
            exit(json_encode([
                'status' => 1,
                'msg'    => '数据校检成功'
            ]));
        }else{
            exit(json_encode([
                'status' => 0,
                'msg'    => '数据校检失败'
            ]));
        }
    }
}
